import tkinter as tk
from .screens.launch_screen import init_launch_screen
from .screens.selection_screen import init_selection_screen
from .screens.creation_screen import init_creation_screen
from .screens.edit_screen import init_edit_screen

root = None
current_screen = None
items = []
screen_initializers = {}
BACKGROUND_MAIN = "#FFD1DC"


def init_main_window():
    """
    Initializes the main application window.
    
    Sets up the Tkinter root window with the title "Electronic Pet Collection"
    and dimensions 800x600. Configures the window to be non-resizable.
    Registers screen initializers for launch, selection, creation, and edit screens.
    Loads initial pet data from "pets.json" and displays the launch screen.
    """
    global root, screen_initializers
    
    root = tk.Tk()
    root.title("Collection Manager (Game Characters)")
    root.geometry("800x600")
    root.resizable(False, False)
    root.configure(bg=BACKGROUND_MAIN)
    
    screen_initializers = {
        "launch": init_launch_screen,
        "selection": init_selection_screen,
        "creation": init_creation_screen,
        "edit": init_edit_screen
    }
    
    show_screen("launch")

def load_initial_data():
    """
    Loads initial pet data from "pets.json" file.
    
    Attempts to load pet data using the load_from_file function from file_manager.
    If successful, stores up to the first 4 items in the global items list.
    If loading fails, initializes items as an empty list.
    """
    global items
    try:
        from src.storage.file_manager import load_from_file
        items = load_from_file("data/pets.json")[:4]
    except:
        items = [] 

def show_screen(screen_name):
    """
    Displays the specified screen by initializing its widgets.
    
    Destroys all existing widgets in the root window, sets the current screen
    to the specified name, and initializes the screen using its corresponding
    initializer function from screen_initializers.
    """
    global current_screen
    for widget in root.winfo_children():
        widget.destroy()
    current_screen = screen_name
    if screen_name in screen_initializers:
        screen_initializers[screen_name](root, items)

def add_item(new_item):
    """
    Adds a new pet item to the collection if space is available.
    
    Appends the new_item to the global items list if the collection
    size is less than 4. Saves the updated items to "pets.json" and
    returns True if successful. Returns False if the collection is full.
    """
    global items
    if len(items) < 4:
        items.append(new_item)
        return True
    return False

def update_items(updated_items):
    """
    Updates the global items list with a new set of items.
    
    Replaces the current items list with the first 4 items from updated_items.
    Saves the updated items to "pets.json".
    """
    global items
    items = updated_items[:4]
    save_items()  

def save_items():
    """
    Saves the current items list to "pets.json" file.
    
    Uses the save_to_file function from file_manager to store the items list.
    """
    from src.storage.file_manager import save_to_file
    save_to_file(items, "data/pets.json")

def run_app():
    """
    Runs the main application loop.
    
    Enters the Tkinter event loop, keeping the application window open
    and responsive to user interactions.
    """
    root.mainloop()
